package com.csdn.leon.view.dialog;

import com.csdn.leon.db.DBUtil;
import com.csdn.leon.base.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class ClickedItemDialog extends Dialog implements
		android.view.View.OnClickListener {
	int layoutRes;// 布局文件
	Context context;

	private String username;
	private String title;
	private TextView text1;
	private TextView text2;
	private TextView text3;
	private View view;
	private boolean isWatcherBlogger;
	private boolean isFavoriteBlog;
	private HomeDialogListener listener;

	private Handler handle = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				text3.setVisibility(View.VISIBLE);
				view.setVisibility(View.VISIBLE);
				text3.setText(R.string.no_favorite_blogger);
			} else if (msg.what == 1) {
				text3.setText(R.string.favorite_blogger);
				//text3.setVisibility(View.GONE);
				//view.setVisibility(View.GONE);
			}else if (msg.what == 2) {
				text2.setVisibility(View.VISIBLE);
				view.setVisibility(View.VISIBLE);
				text2.setText(R.string.no_favorite_blog);
			} else if (msg.what == 3) {
				text2.setText(R.string.favorite_blog);
				//text3.setVisibility(View.GONE);
				//view.setVisibility(View.GONE);
			}
		}
	};

	public ClickedItemDialog(Context context) {
		super(context);
	}

	/**
	 * 自定义布局的构造方法
	 * 
	 * @param context
	 * @param resLayout
	 */
	public ClickedItemDialog(Context context, int resLayout) {
		super(context);
		this.context = context;
		this.layoutRes = resLayout;
	}

	/**
	 * 自定义主题及布局的构造方法
	 * 
	 * @param context
	 * @param theme
	 * @param resLayout
	 */
	public ClickedItemDialog(Context context, int theme, int resLayout,
			String username, String title) {
		super(context, theme);
		this.context = context;
		this.layoutRes = resLayout;
		this.username = username;
		this.title = title;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(layoutRes);
		setCancelable(true);
		initView();
	}

	private void initView() {
		text1 = (TextView) findViewById(R.id.text1);
		text2 = (TextView) findViewById(R.id.text2);
		text3 = (TextView) findViewById(R.id.text3);
		view = (View) findViewById(R.id.view);
		new Thread(new Runnable() {

			@Override
			public void run() {
				isWatcherBlogger = DBUtil.queryHost(context, username);
				isFavoriteBlog = DBUtil.queryBlogModel(context, title);
				if (isWatcherBlogger) {
					Message msg = new Message();
					msg.what = 0;
					handle.sendMessage(msg);
				} else {
					Message msg = new Message();
					msg.what = 1;
					handle.sendMessage(msg);
				}
				if (isFavoriteBlog) {
					Message msg = new Message();
					msg.what = 2;
					handle.sendMessage(msg);
				} else {
					Message msg = new Message();
					msg.what = 3;
					handle.sendMessage(msg);
				}
			}
		}).start();
		text1.setOnClickListener(this);
		text2.setOnClickListener(this);
		text3.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.text1:
			listener.onClicked(1, true);
			break;
		case R.id.text2:
			listener.onClicked(2, isFavoriteBlog);
			break;
		case R.id.text3:
			listener.onClicked(3, isWatcherBlogger);
			break;
		default:
			break;
		}

	}

	public interface HomeDialogListener {
		public void onClicked(int id, boolean flag);
	}

	public void setListener(HomeDialogListener listener) {
		this.listener = listener;
	}
}
